import Cookies from 'js-cookie';

const TokenKey = 'Admin-Token';
const RefreshTokenKey = 'Admin-Refresh-Token';
const ExpiresInKey = 'Admin-Expires-In';

export function getToken() {
    return Cookies.get(TokenKey);
}

export function setToken(token: string) {
    return Cookies.set(TokenKey, token);
}

export function removeToken() {
    return Cookies.remove(TokenKey);
}

export function getExpiresIn() {
    return Cookies.get(ExpiresInKey);
}

export function setExpiresIn(time: string) {
    return Cookies.set(ExpiresInKey, time);
}

export function removeExpiresIn() {
    return Cookies.remove(ExpiresInKey);
}

export function getRefreshToken() {
    return Cookies.get(RefreshTokenKey) || '';
}

export function setRefreshToken(token: string) {
    return Cookies.set(RefreshTokenKey, token);
}

export function removeRefreshToken() {
    return Cookies.remove(RefreshTokenKey);
}

// 存储本地存储
export function setLStorage(key: string, val: any) {
    window.localStorage.setItem(key, val);
}

// 获取本地存储，如果存在第二个参数则转换成JSON
export function getLStorage(key: string, flag?: boolean) {
    return flag
        ? JSON.parse(window.localStorage.getItem(key) as any)
        : window.localStorage.getItem(key);
}

// 删除本地存储
export function delLStorage(key: string) {
    window.localStorage.removeItem(key);
}

// 清空本地存储
export function clearLStorage() {
    window.localStorage.clear();
}

// 存储本地存储
export function setSStorage(key: string, val: any) {
    window.sessionStorage.setItem(key, val);
}

// 获取本地存储，如果存在第二个参数则转换成JSON
export function getSStorage(key: string, flag?: boolean) {
    return flag
        ? JSON.parse(window.sessionStorage.getItem(key) as any)
        : window.sessionStorage.getItem(key);
}

// 删除本地存储
export function delSStorage(key: string) {
    window.sessionStorage.removeItem(key);
}

// 清空本地存储
export function clearSStorage() {
    window.sessionStorage.clear();
}
